package com.zhongxi.guoyuan.view.user;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.UserData;
import com.zhongxi.guoyuan.entity.user.UseInfo;
import com.zhongxi.guoyuan.entity.user.UserResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.view.MainHomeActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private Button btSure;
    private EditText etPassword;
    private String trim;
    private String type;
    private String mId;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_set_password);
        initTitle();
        tvTitle.setText("设置密码");
        btSure = (Button) findViewById(R.id.btSure);
        etPassword = (EditText) findViewById(R.id.etPassword);
        if (getIntent() != null) {
            mId = getIntent().getStringExtra("id");
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        btSure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.btSure:
                trim = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    showShortToast("密码不能为空");
                } else {
                    Map<String, String> map = new HashMap<String, String>();
                    map.clear();
                    map.put("useid", mId);
                    map.put("newpwd", MyApplication.getInstance().descsc(etPassword.getText().toString().trim()));
                    map.put("oldpwd", "");
                    type = "setSecret";
                    getServer(Url.SET_PWD, map, "", false, null, "post");
                }
                break;
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("setSecret".equals(type)) {
                UserResult result = gson.fromJson(response, UserResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (null != result.getData() && !"".equals(result.getData())) {
                    UserData data = result.getData();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("phone", result.getData().getMphone()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("userid", result.getData().getId()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("username", result.getData().getUsername()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("shopname", result.getData().getShopname()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("avatar", result.getData().getAvatar()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("time", result.getData().getCtime()).commit();
                    MyApplication.getInstance().sharedPrefrences.edit().putString("appopenid", result.getData().getAppopenid()).commit();
                    if (null != data.getIsshareholders() && !"".equals(data.getIsshareholders())) {  //股东
                        MyApplication.getInstance().sharedPrefrences.edit().putBoolean("shareHolder", "1".equals(data.getIsshareholders())).commit();
                    }
                    if (null != data.getIsshareholders() && !"".equals(data.getIsshareholders())) {  // 消费收益人
                        MyApplication.getInstance().sharedPrefrences.edit().putBoolean("beneficiary", "1".equals(data.getIsbeneficiary())).commit();
                    }
                    if (null != data.getIsshareholders() && !"".equals(data.getIsshareholders())) {  //商家
                        MyApplication.getInstance().sharedPrefrences.edit().putBoolean("merchants", "1".equals(data.getIsmerchants())).commit();
                    }

                    if (null != String.valueOf(data.getIspartner())) {
                        MyApplication.getInstance().sharedPrefrences.edit().putBoolean("partener", 1 == data.getIspartner()).commit();
                    }
                    JPushInterface.setAlias(context, MyApplication.getInstance().getUserId(), new TagAliasCallback() {
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            if (i == 0) {
                                Log.d("LoginActivity", s);
                            }
                        }
                    });
                }
                if (null != result.getExtend() && !"".equals(result.getExtend())) {
                    UseInfo extend = result.getExtend();
                    if (null != extend.getInnercurrency() && !"".equals(extend.getInnercurrency())) {
                        MyApplication.getInstance().sharedPrefrences.edit().putString("inner", extend.getInnercurrency()).commit();
                    }
                    if (null != String.valueOf(extend.getId())) {
                        MyApplication.getInstance().sharedPrefrences.edit().putString("num", extend.getId() + "").commit();   //用户编号
                    }
                    if (null != extend.getSharesgains() && !"".equals(extend.getSharesgains())) {
                        MyApplication.getInstance().sharedPrefrences.edit().putString("share", DataUtil.formatData(extend.getSharesgains())).commit();  //股份收益
                    }
                }
                finish();
                startAct(MainHomeActivity.class);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        try {
            Gson gson = new Gson();
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (result.getCode() == 1000) {
                showShortToast("参数错误");
            } else if (result.getCode() == 1999) {
                showShortToast("Url 无效");
            } else if (result.getCode() == 4003) {
                showShortToast("老密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
